﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace Socket服务器
{
    public class ServerSource
    {

        public SocketManager NewAddSocketManager;

        public SocketCollection SCollection = new SocketCollection();



        /// <summary>
        /// 是否允许新增
        /// </summary>
        /// <param name="Port">要新增的端口号</param>
        /// <param name="IP">要新增的IP地址</param>
        /// <returns>不存在返回false，存在返回true</returns>
        public bool IsExist(int Port)
        {
            int query = SCollection.Where(C => C.Port == Port ).Count();

            if (query > 0)
                return true;
            return false;
        }


        /// <summary>
        /// 新增成功返回TRue
        /// </summary>
        /// <param name="S">新增Server的配置文件</param>
        /// <returns></returns>
        public bool ServerAdd(ServerConfig S)
        {
            if (!IsExist(S.ServerPort ))
            {
                SocketManager ServerView = new SocketManager(S);
                //Socketview.Add(S.ServerPort, ServerView);
                SCollection.Add(ServerView);
                NewAddSocketManager = ServerView;
                return true;
            }
            else return false;

        }

        /// <summary>
        /// 得到一个server模型
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        public SocketManager GetServer(int port)
        {
            int query = SCollection.Where(c => c.Port == port).Count();

            if (query > 0)
                return SCollection.Where(C => C.Port == port).FirstOrDefault();
            else
                return null;
        }

/// <summary>
/// 删除一个服务
/// </summary>
/// <param name="port"></param>
/// <returns></returns>
        public bool DeleteServer(int port)
        {
            if (IsExist(port ))
            {
                SocketManager SM = SCollection.Where(c => c.Port == port).FirstOrDefault();
                SCollection.Remove(SM);
                //SM.socketserver.SocketAsyncPool.DisPoseSocketPool();
                //SM.socketserver.listenSocket.Dispose();
                //SM.dispose();
                return true;
            }
            return false;
        }
    }

    }
